package com.example.testlibraryproject.webservices;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import android.util.Log;

public class WebserviceMain {
	
	private static final String logTag = "##WS##";
	/** Request time out time in milliseconds */
	public static final int TIMEOUT_INTERVAL = 5000;
	
	/**
	 * Function to be used to work on to execute GET methods, should be called from non UI thread
	 * @param url  String url to be used ;
	 * @return String format of data fetched, null if error occurred
	 */
	public static String executeGet(String url){
		return executeGet(url, null);
	}
	
	/**
	 * Function to be used to work on to execute GET methods, should be called from non UI thread
	 * @param url  String url to be used ;
	 * @param params ArrayList<BasicNameValuePair> key values pairs to be used pass null if no parameters specified
	 * @return String format of data fetched, null if error occurred
	 */
	public static String executeGet(String url, ArrayList<BasicNameValuePair> params){

		Log.v(logTag,"uri initially is:"+url);

		/* adding params if specified */
		if(params!=null && !params.isEmpty()){

			if(!url.endsWith("?")){
				url += "?";
			}
			url += URLEncodedUtils.format(params, "utf-8");
		}

		Log.v(logTag,"uri finally is:"+url);

		String wsResult = null;
		HttpGet httpGet = new HttpGet(url);

		try {

			Log.v(logTag,"callling httpget");
			HttpParams httpParams = new BasicHttpParams();
			HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_INTERVAL);
			HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_INTERVAL);
			HttpClient httpClient = new DefaultHttpClient(httpParams);
			
			/*///////////code to set cookies in request
			httpGet.setHeader("Cookie", "name1=value1; name2=value2");
			///////////////////
			*/			
			HttpResponse responseGet = httpClient.execute(httpGet);
			final int statusCode = responseGet.getStatusLine().getStatusCode();
			Log.v(logTag,"executeGet get response::"+responseGet.getStatusLine().getStatusCode()+
					", "+responseGet.getStatusLine().getReasonPhrase());
			if (statusCode != HttpStatus.SC_OK) {
				Log.v(logTag,"Error " + statusCode + " for URL " + url);
				return null;
			}

			HttpEntity responseEntity = responseGet.getEntity();
			wsResult = EntityUtils.toString(responseEntity);
			Log.v(logTag,"WS RESULt:: "+wsResult);
			return wsResult;

		} catch (ClientProtocolException e) {
			e.printStackTrace();
			Log.v(logTag,"WS error:"+e.getMessage());
		} catch (IOException e) {
			e.printStackTrace();
			Log.v(logTag,"WS error:"+e.getMessage());
		}


		return wsResult;
	}

	/**
	 * Function to be used to work on to execute POST methods, should be called from non UI thread
	 * @param url  String url to be used ;
	 * @return String format of data fetched, null if error occurred
	 */
	public static String executePost(String url){
		return executePost(url, null);
	}
	/**
	 * Function to be used to work on to execute POST methods, should be called from non UI thread
	 * @param url  String url to be used ;
	 * @param params ArrayList<BasicNameValuePair> key values pairs to be used pass null if no parameters specified
	 * @return String format of data fetched, null if error occurred
	 */
	public static String executePost(String url, ArrayList<BasicNameValuePair> params){

		HttpPost httpPost = new HttpPost(url);

		if(params!=null && !params.isEmpty()){
			try {
				httpPost.setEntity(new UrlEncodedFormEntity(params));
			} catch (UnsupportedEncodingException e1) {
				e1.printStackTrace();
			}
		}

		String wsResult = null;

		try {
			HttpParams httpParams = new BasicHttpParams();
			HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_INTERVAL);
			HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_INTERVAL);
			HttpClient httpClient = new DefaultHttpClient(httpParams);
			
			/* ///////////code to set cookies in request
			httpPost.setHeader("Cookie", "name1=value1; name2=value2");
			/////////////////// */
			HttpResponse responseGet = httpClient.execute(httpPost);
			
			
			final int statusCode = responseGet.getStatusLine().getStatusCode();
			Log.v(logTag,"executePost response::"+responseGet.getStatusLine().getStatusCode()+", "+responseGet.getStatusLine().getReasonPhrase());
			if (statusCode != HttpStatus.SC_OK) {
				Log.v(logTag,"Error " + statusCode + " for URL " + url);
				return null;
			}

			HttpEntity responseEntity = responseGet.getEntity();
			wsResult = EntityUtils.toString(responseEntity);
			Log.v(logTag,"WS RESULt:: "+wsResult);
			return wsResult;

		} catch (ClientProtocolException e) {
			e.printStackTrace();
			Log.v(logTag,"WS error:"+e.getMessage());
		} catch (IOException e) {
			e.printStackTrace();
			Log.v(logTag,"WS error:"+e.getMessage());
		}

		return wsResult;
	}
	
	


}
